using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace XZ.Logistics.WebShop
{
    public class ShippingMethod : WebShopBase
   {
      private string _title = "";
      public string Title
      {
         get { return _title; }
         set { _title = value; }
      }

      private decimal _price = 0.0m;
      public decimal Price
      {
         get { return _price; }
         set { _price = value; }
      }

      public string TitleAndPrice
      {
         get
         {
            return string.Format("{0} ({1:N2} {2})", 
               this.Title, this.Price, Settings.CurrencyCode);
         }
      }

      public ShippingMethod(Guid id, DateTime addedDate, string addedBy, string title, decimal price)
      {
         this.Id = id;
         this.AddedDate = addedDate;
         this.AddedBy = addedBy;
         this.Title = title;
         this.Price = price;
      }

      public bool Delete()
      {
         bool success = ShippingMethod.DeleteShippingMethod(this.Id);
         if (success)
            this.Id = Guid.Empty;
         return success;
      }

      public bool Update()
      { 
         return ShippingMethod.UpdateShippingMethod(this.Id, this.Title, this.Price);
      }

      /***********************************
      * Static methods
      ************************************/

      /// <summary>
      /// Returns a collection with all the shipping methods
      /// </summary>
      public static List<ShippingMethod> GetShippingMethods()
      {
         List<ShippingMethod> shippingMethods = null;
         string key = "Store_ShippingMethods";

         if (WebShopBase.Settings.EnableCaching && BizObject.Cache[key] != null)
         {
            shippingMethods = (List<ShippingMethod>)BizObject.Cache[key];
         }
         else
         {
             shippingMethods = WebShop.GetShippingMethods();
            WebShopBase.CacheData(key, shippingMethods);            
         }
         return shippingMethods;
      }

      /// <summary>
      /// Returns a ShippingMethod object with the specified ID
      /// </summary>
      public static ShippingMethod GetShippingMethodByID(Guid shippingMethodId)
      {
         ShippingMethod shippingMethod = null;
         string key = "Store_ShippingMethod_" + shippingMethodId.ToString();

         if (WebShopBase.Settings.EnableCaching && BizObject.Cache[key] != null)
         {
            shippingMethod = (ShippingMethod)BizObject.Cache[key];
         }
         else
         {
             shippingMethod = WebShop.GetShippingMethodById(shippingMethodId);
             WebShopBase.CacheData(key, shippingMethod);
         }
         return shippingMethod; 
      }

      /// <summary>
      /// Updates an existing shipping method
      /// </summary>
      public static bool UpdateShippingMethod(Guid id, string title, decimal price)
      {
         ShippingMethod record = new ShippingMethod(id, DateTime.Now, "", title, price);
         bool ret = WebShop.UpdateShippingMethod(record);
         BizObject.PurgeCacheItems("WebSop_Shippingmethod");
         return ret;
      }

      /// <summary>
      /// Deletes an existing shipping method
      /// </summary>
      public static bool DeleteShippingMethod(Guid id)
      {
          bool ret = WebShop.DeleteShippingMethod(id);
         //new RecordDeletedEvent("shipping method", id, null).Raise();
          BizObject.PurgeCacheItems("WebSop_Shippingmethod");
         return ret;
      }

      /// <summary>
      /// Creates a new shipping method
      /// </summary>
      public static Guid InsertShippingMethod(string title, decimal price)
      {
         ShippingMethod record = new ShippingMethod(Guid.Empty, DateTime.Now,
            BizObject.CurrentUserName, title, price);
         Guid ret = WebShop.InsertShippingMethod(record);
         BizObject.PurgeCacheItems("WebSop_Shippingmethod");
         return ret;
      }
   }
}